package com.oe.market.modules.common.model;

import java.sql.*;

public class BaseDao {
    private static String driver="com.mysql.cj.jdbc.Driver";
    private static String url="jdbc:mysql://47.98.173.197:3306/javaweb?characterEncoding=utf8&useSSL=true&serverTimezone=UTC";
    private static String user="root";
    private static String password="123456";
    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

    public static void closeAll(Connection conn,Statement stmt,ResultSet rs) throws SQLException {
        if(rs!=null) {
            rs.close();
        }
        if(stmt!=null) {
            stmt.close();
        }
        if(conn!=null) {
            conn.close();
        }
    }


    protected int executeSql(String sql) throws SQLException {
        Connection conn = getConnection();
        Statement statement = conn.createStatement();
        int ret = statement.executeUpdate(sql);
        closeAll(conn,statement,null);
        return ret;
    }

    //////////////////////// sql //////////////////////////////

    protected <T> String KeyValueEquation(String key, String symbol, T value) {
        return " `" + key + "`" + symbol + "'" + value + "' ";
    }

    protected <T> String KeyValueEquation(String key, T value) {
        return " `" + key + "` = '" + value + "' ";
    }

}
